﻿namespace NeuralNetworks.Neurons
{
	/// <summary>
	/// Y = mx + C
	/// </summary>
	public class SimpleNeuron : NeuronBase
	{
		#region Private Fields

		private double _learningCoef = 0.1;


		private double _c;

		#endregion

		#region Properties

		public double LearningCoef
		{
			get { return _learningCoef; }
			set { _learningCoef = value; }
		}

		public double C
		{
			get { return _c; }
			set { _c = value; }
		}

		public double M
		{
			get { return this[0]; }
			set { this[0] = value; }
		}

		#endregion

		#region Constructor

		public SimpleNeuron() : base(1) { }

		#endregion

		#region Public Methods

		#region Overrides of NeuronBase

		public override double Compute(double[] input)
		{
			output = input[0] * this[0] + _c;
			return output;
		}

		public override void RandomizeWeigths()
		{
			base.RandomizeWeigths();
			_c = RandomGenerator.NextDouble() *
			     (System.Math.Abs(RandomGeneratorRange.Min) + System.Math.Abs(RandomGeneratorRange.Max)) +
			     RandomGeneratorRange.Min;
		}

		#endregion

		public void Learn(double input, double result)
		{
			Compute(new[] { input });
			M = M + LearningCoef * (result - Output) * input;
			C = C + LearningCoef * (result - Output);
		}

		#endregion
	}
}